import random
import time

from machine import Pin, I2C

import sh1106

lcd_width = 128
lcd_height = 64

sda = Pin(21)
scl = Pin(22)
i2c = I2C(0, scl=scl, sda=sda, freq=400000)

lcd_devices = i2c.scan()
if len(lcd_devices) == 0:
    print("No i2c device !")
else:
    print('i2c devices found:', len(lcd_devices))
    lcd = sh1106.SH1106_I2C(lcd_width, lcd_height, i2c)
    lcd.sleep(False)
    lcd.rotate(True)

size = 40
l_x = int(lcd_width / 4 - size / 2)
r_x = int(lcd_width / 2 + l_x)
l_y = int((lcd_height - size) / 2)
r_y = l_y


def init_eyes():
    lcd.fill(0)
    lcd.fill_rect(l_x, l_y, size, size, 1)
    lcd.fill_rect(r_x, r_y, size, size, 1)
    lcd.show()


def smail():
    lcd.fill(0)
    middle_line_h = lcd_height / 2
    middle_offset = 10
    h_step = 3
    w_step = 4
    pixels = 10
    for i in range(pixels + 1):
        px = l_x + i * w_step
        flag = i
        if i >= pixels / 2:
            flag = (pixels - i)
        py = middle_line_h - middle_offset - flag * h_step

        pixel_block(px, py)
        pixel_block(px + lcd_width / 2, py)
    lcd.show()


def happy():
    lcd.fill(0)
    middle_line_h = lcd_height / 2
    middle_offset = 20
    h_step = 3
    w_step = 4
    pixels = 10
    x_start = 20
    temp_h_offset = middle_line_h - middle_offset
    for i in range(pixels + 1):
        flag = 1
        if i >= (pixels + 1) / 2:
            flag = -1
        px = x_start + flag * w_step
        x_start = px

        py = temp_h_offset + h_step
        temp_h_offset = py

        pixel_block(px, py)
        pixel_block(lcd_width - px, py)
    lcd.show()


def anger():
    lcd.fill(0)
    middle_line_h = lcd_height / 2
    middle_offset = 20
    h_step = 2
    w_step = 4
    pixels = 10
    temp_h_offset = middle_line_h - middle_offset
    for i in range(pixels + 1):
        px = l_x + i * w_step
        py = temp_h_offset + h_step
        temp_h_offset = py

        pixel_block(px, py)
        pixel_block(lcd_width - px, py)
    lcd.show()


def cry():
    lcd.fill(0)
    middle_line_h = lcd_height / 2
    middle_offset = 20
    h_step = 2
    w_step = 4
    pixels = 10
    line_h = middle_line_h - middle_offset
    for i in range(pixels + 1):
        px = l_x + i * w_step
        pixel_block(px, line_h)
        pixel_block(lcd_width - px, line_h)

    # 绘制眼泪动效
    pixel_block(30, 30, 14)
    pixel_block(30, 50, 14)
    pixel_block(90, 30, 14)
    pixel_block(90, 50, 14)
    lcd.show()


def pixel_block(x, y, pixel_size=8):
    lcd.fill_rect(int(x), int(y), pixel_size, pixel_size, 1)


def blink():
    init_eyes()
    frames = 10
    for i in range(1, frames):
        lcd.fill(0)
        step = size / frames
        move = int(i * 2 * step)
        re_height = size - move
        re_ly = int(lcd_height / 2 - re_height / 2)

        lcd.fill_rect(l_x, re_ly, size, re_height, 1)
        lcd.fill_rect(r_x, re_ly, size, re_height, 1)
        lcd.show()
        time.sleep_ms(5)
    init_eyes()


def random_weights(weights):
    total = sum(weights.values())
    seed = random.randint(0, total)
    temp_flag = 0
    for k, v in weights.items():
        target_range = temp_flag + v
        temp_flag = target_range
        if seed <= target_range:
            return k
